package com.swpu.TestController;

import com.swpu.Pojo.Books;
import com.swpu.Services.BookServices;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * 功能描述：
 *
 * @Author: ygq
 * @Date: 2021/2/19 18:40
 */
@Controller
@RequestMapping("/book")
public class BookController {
    @Qualifier("bookServicesImpl")
    @Autowired
    private BookServices bookServices;


    //查询全部的书
    @RequestMapping("/AllBook")
    public String ListBook(HttpServletRequest request){
        List<Books> booksList = bookServices.QueryAllBooks();
        request.setAttribute("booklist",booksList);
//        System.out.println(booksList);
        return "AllBook";
    }

    //跳转到增加书籍的页面
    @RequestMapping("/ToAddBookPage")
    public String ToAddBook(){

        return "AddBook";
    }
    @RequestMapping("addBook")
    public String AddBook(Books books){
        System.out.println(books);
        bookServices.addBook(books);
        return "redirect:/book/AllBook";
    }

    @RequestMapping("/toUpdateBook")
    public String toUpdate(HttpSession session, int id){
        Books bo = bookServices.QueryBookById(id);
        session.setAttribute("book",bo);
        System.out.println("修改的书籍是"+bo);

        return "updateBook";
    }
    @RequestMapping("/update")
    public String update(Books books){
        bookServices.updateBook(books);
        List<Books> booksList = bookServices.QueryAllBooks();
        return "redirect:/book/AllBook";
    }
    @RequestMapping("/delete/{bookID}")
    public String delete( @PathVariable("bookID")int id){
        bookServices.deleteBookById(id);
        return "redirect:/book/AllBook";
    }

    @RequestMapping("/QueryBook")
    public String QueryBook(@Param("bookName") String queryName, Model model) {
        List<Books> list = bookServices.QueryBook(queryName);
        if (list.isEmpty()) {
            model.addAttribute("msg", "  没有查找数据。。。");
        }
        model.addAttribute("booklist", list);
        return "AllBook";
    }
}
